﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace Common.UI.Controls
{
    [ContentProperty("ContentControl")]
    /// <summary>
    /// Interaction logic for LabelControl.xaml
    /// </summary>
    public partial class LabelControl : UserControl, IControlWidth
    {
        public LabelControl()
        {
            InitializeComponent();
            this.GotFocus += new RoutedEventHandler(LabelControl_GotFocus);
        }

        void LabelControl_GotFocus(object sender, RoutedEventArgs e)
        {
            var element = this.contentControl.Content as UIElement;
            element.Focus();
        }

        public Object ContentControl
        {
            get { return (Object)GetValue(ContentControlProperty); }
            set { SetValue(ContentControlProperty, value); }
        }

        public static readonly DependencyProperty ContentControlProperty =
            DependencyProperty.Register("ContentControl", typeof(Object), typeof(LabelControl), new UIPropertyMetadata(null, new PropertyChangedCallback(ContentControl_Changed)));

        private static void ContentControl_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            LabelControl labelControl = (LabelControl)o;
            labelControl.contentControl.Content = e.NewValue;
        }

        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(String), typeof(LabelControl), new UIPropertyMetadata(null, new PropertyChangedCallback(Text_Changed)));

        private static void Text_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            LabelControl labelControl = (LabelControl)o;
            labelControl.textBlock.Text = (string)e.NewValue;
        }

        #region IControlWidth Members

        public double LabelWidth
        {
            get
            {
                return this.textBlock.ActualWidth;
            }
            set
            {
                this.textBlock.Width = value;
            }
        }

        #endregion
    }
}
